
package com.ami.kvm.jviewer.hid;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.gui.JVMenu;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import java.awt.event.KeyEvent;
import java.io.PrintStream;
import java.util.HashMap;
import javax.swing.JMenuItem;

public class USBCrossMapKeyProcessor extends USBKeyProcessorEnglish {
	boolean shiftModified = false;
	boolean altGrModified = false;
	boolean leftCTRLModified = false;
	boolean leftCTRLHeldDown = false;
	boolean rightCTRLModified = false;
	boolean rightCTRLHeldDown = false;
	boolean altGrHeldDown = false;
	boolean shiftHeldDown = false;
	boolean autoKeyBreakMode = false;
	byte[] usbKeyRepPkt = null;
	private int pressedKeyCode = 0;
	private int pressedKeyChar = 0;
	protected boolean autoKeybreakModeOn = false;
	protected HashMap<Integer, Integer> localKeyCodeMap;
	protected int hostLayoutIndex = -1;
	protected final int[] commonKeys = { 27, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 61440, 61441,
			61442, 61443, 61444, 61445, 61446, 61447, 61448, 61450, 61451, 9, 20, 16, 17, 18, 32, 65406, 524, 525, 10,
			8, 154, 145, 19, 155, 36, 33, 127, 35, 34, 144, 38, 40, 37, 39, 96, 97, 98, 99, 100, 101, 102, 103, 104,
			105, 28, 29, 240, 241, 242, 243, 244, 263 };

	protected final HashMap<Integer, Integer> keyCodeMap;

	public USBCrossMapKeyProcessor() {
		this.keyCodeMap = new HashMap() {
		};
	}

	public byte[] convertKeyCode(int paramInt1, int paramInt2, boolean paramBoolean, char paramChar) {
		Integer localInteger = new Integer(0);
		byte[] arrayOfByte = new byte[6];
		boolean bool = false;
		int i = 0;
		this.autoKeyBreakMode = this.autoKeybreakModeOn;

		i = convertToHostLayout(paramInt1, paramBoolean, paramChar);

		switch (paramInt2) {
		case 2:
			if (i != 0) {
				localInteger = (Integer) this.leftMap.get(Integer.valueOf(i));
			} else if (paramInt1 != 0) {
				localInteger = (Integer) this.leftMap.get(Integer.valueOf(paramInt1));
			}
			break;
		case 4:
			if (i == 46) {
				localInteger = (Integer) this.standardMap.get(Integer.valueOf(i));
			} else if (i == 44) {
				localInteger = (Integer) this.standardMap.get(Integer.valueOf(i));
			} else {
				localInteger = (Integer) this.standardMap.get(Integer.valueOf(paramInt1));
				if (localInteger == null) {
					localInteger = (Integer) this.keypadMap.get(Integer.valueOf(paramInt1));
				}
			}
			break;
		case 3:
			if (i != 0) {
				localInteger = (Integer) this.rightMap.get(Integer.valueOf(i));
			} else if (paramInt1 != 0) {
				localInteger = (Integer) this.rightMap.get(Integer.valueOf(paramInt1));
			}
			break;
		case 1:
			if (i != 0) {
				localInteger = (Integer) this.standardMap.get(Integer.valueOf(i));
			} else if (paramInt1 != 0) {
				localInteger = (Integer) this.standardMap.get(Integer.valueOf(paramInt1));
			}
			break;
		case 0:
			if (paramBoolean) {
				if (i != 0) {
					localInteger = (Integer) this.standardMap.get(Integer.valueOf(i));
				} else if (paramInt1 != 0)
					localInteger = (Integer) this.standardMap.get(Integer.valueOf(paramInt1));
			} else {
				System.err.println(LocaleStrings.getString("AD_1_USBKP"));
			}
			break;

		default:
			System.err.println(LocaleStrings.getString("AD_2_USBKP"));
		}

		if (localInteger != null) {
			switch (i) {
			case 17:
				if (paramInt2 == 2) {
					if (paramBoolean) {
						modifiers = (byte) (modifiers | 0x1);
						this.leftCTRLHeldDown = true;
					} else {
						this.leftCTRLHeldDown = false;
						modifiers = (byte) (modifiers & 0xFFFFFFFE);
					}

				} else if (paramBoolean) {
					modifiers = (byte) (modifiers | 0x10);
					this.rightCTRLHeldDown = true;
				} else {
					this.rightCTRLHeldDown = false;
					modifiers = (byte) (modifiers & 0xFFFFFFEF);
				}

				break;

			case 16:
				if (paramInt2 == 2) {
					if (paramBoolean) {
						modifiers = (byte) (modifiers | 0x2);
						this.shiftHeldDown = true;
					} else {
						this.shiftHeldDown = false;
						modifiers = (byte) (modifiers & 0xFFFFFFFD);
						modifiers = (byte) (modifiers & 0xFFFFFFDF);
					}
				} else if (paramBoolean) {
					modifiers = (byte) (modifiers | 0x20);
					this.shiftHeldDown = true;
				} else {
					this.shiftHeldDown = false;
					modifiers = (byte) (modifiers & 0xFFFFFFDF);
					modifiers = (byte) (modifiers & 0xFFFFFFFD);
				}

				break;

			case 18:
				if (paramInt2 == 2) {
					if (paramBoolean) {
						modifiers = (byte) (modifiers | 0x4);
					} else {
						modifiers = (byte) (modifiers & 0xFFFFFFFB);
					}
				} else if (paramBoolean) {
					modifiers = (byte) (modifiers | 0x40);
					this.altGrHeldDown = true;
				} else {
					this.altGrHeldDown = false;
					modifiers = (byte) (modifiers & 0xFFFFFFBF);
				}

				break;

			case 524:
				if (paramInt2 == 2) {
					if (paramBoolean) {
						modifiers = (byte) (modifiers | 0x8);
					} else {
						modifiers = (byte) (modifiers & 0xFFFFFFF7);
					}
				} else if (paramBoolean) {
					modifiers = (byte) (modifiers | 0x80);
				} else {
					modifiers = (byte) (modifiers & 0xFF7F);
				}
				break;

			default:
				if ((paramBoolean) || (i == 154)) {
					arrayOfByte[0] = localInteger.byteValue();
					if (this.autoKeybreakModeOn) {
						bool = true;
					}
				} else if ((!this.autoKeybreakModeOn) || (i == 144) || (i == 20) || (i == 145)) {

					arrayOfByte[0] = 0;
				} else {
					this.autoKeybreakModeOn = this.autoKeyBreakMode;
					return null;
				}

				break;
			}

			USBKeyProcessorEnglish.setModifiers(modifiers);
			this.usbKeyRepPkt = USBKeyboardRepPkt(arrayOfByte, modifiers, bool);
			this.autoKeybreakModeOn = this.autoKeyBreakMode;
			return this.usbKeyRepPkt;
		}
		Debug.out.println(LocaleStrings.getString("AD_3_USBKP") + KeyEvent.getKeyText(i));
		this.autoKeybreakModeOn = this.autoKeyBreakMode;
		return null;
	}

	private int convertToHostLayout(int paramInt, boolean paramBoolean, char paramChar) {
		Integer localInteger = null;
		int i = -1;
		if (paramBoolean) {

			if ((JViewerApp.getInstance().getClientKeyboardLEDStatus() & 0x2) == 2) {
				i = shiftedCapsCharSet[this.hostLayoutIndex].indexOf(paramChar);
				if (i >= 0) {

					if ((this.hostLayoutIndex == 3) || (this.hostLayoutIndex == 20) || (this.hostLayoutIndex == 19)) {

						i++;
					} else if (this.hostLayoutIndex == 16) {
						if (i == shiftedCharSet[this.hostLayoutIndex].indexOf(" ")) {
							i = -1;
						} else {
							i++;
						}
					}
					localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
					if ((paramInt == 92) && (this.hostLayoutIndex == 16)) {
						localInteger = Integer.valueOf(220);
					}
					if (((modifiers & 0x2) != 2) || ((modifiers & 0x20) != 32)) {
						enableShift(true);
						this.shiftModified = true;
					}

					if (((modifiers & 0x40) == 64) && (!JViewerApp.getInstance().getJVMenu()
							.getMenuItem("KeyboardHoldRightAltKey").isSelected())) {
						enableAltGr(false);
						this.altGrModified = true;
					}

					if (((modifiers & 0x1) == 1) && (!JViewerApp.getInstance().getJVMenu()
							.getMenuItem("KeyboardHoldLeftCtrlKey").isSelected())) {
						enableLeftCTRL(false);
						this.leftCTRLModified = true;
					}
				} else {
					i = -1;

					i = normalCapsCharSet[this.hostLayoutIndex].indexOf(paramChar);
					if (i >= 0) {

						if ((this.hostLayoutIndex == 16) || (this.hostLayoutIndex == 20)
								|| (this.hostLayoutIndex == 19)) {

							i++;
						}
						localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
						if ((paramInt == 92) && (this.hostLayoutIndex == 16)) {
							localInteger = Integer.valueOf(220);
						}
						if (((modifiers & 0x2) == 2) || ((modifiers & 0x20) == 32)) {
							enableShift(false);
							this.shiftModified = true;
						}

						if (((modifiers & 0x40) == 64) && (!JViewerApp.getInstance().getJVMenu()
								.getMenuItem("KeyboardHoldRightAltKey").isSelected())) {
							enableAltGr(false);
							this.altGrModified = true;
						}

						if (((modifiers & 0x1) == 1) && (!JViewerApp.getInstance().getJVMenu()
								.getMenuItem("KeyboardHoldLeftCtrlKey").isSelected())) {
							enableLeftCTRL(false);
							this.leftCTRLModified = true;
						}
					}
				}
			} else {
				i = shiftedCharSet[this.hostLayoutIndex].indexOf(paramChar);
				if (i >= 0) {

					if ((this.hostLayoutIndex == 3) || (this.hostLayoutIndex == 20) || (this.hostLayoutIndex == 19)) {

						i++;
					} else if (this.hostLayoutIndex == 16) {
						if (i == shiftedCharSet[this.hostLayoutIndex].indexOf(" ")) {
							i = -1;
						} else {
							i++;
						}
					}
					localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
					if ((paramInt == 92) && (this.hostLayoutIndex == 16)) {
						localInteger = Integer.valueOf(220);
					}
					if (((modifiers & 0x2) != 2) || ((modifiers & 0x20) != 32)) {
						enableShift(true);
						this.shiftModified = true;
					}

					if (((modifiers & 0x40) == 64) && (!JViewerApp.getInstance().getJVMenu()
							.getMenuItem("KeyboardHoldRightAltKey").isSelected())) {
						enableAltGr(false);
						this.altGrModified = true;
					}

					if (((modifiers & 0x1) == 1) && (!JViewerApp.getInstance().getJVMenu()
							.getMenuItem("KeyboardHoldLeftCtrlKey").isSelected())) {
						enableLeftCTRL(false);
						this.leftCTRLModified = true;
					}
					if ((this.hostLayoutIndex == 16) && (i == -1)) {
						enableShift(false);
						this.shiftModified = false;
					}
				} else {
					i = -1;

					i = normalCharSet[this.hostLayoutIndex].indexOf(paramChar);
					if (i >= 0) {

						if ((this.hostLayoutIndex == 16) || (this.hostLayoutIndex == 20)
								|| (this.hostLayoutIndex == 19)) {

							i++;
						}
						localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
						if ((paramInt == 92) && (this.hostLayoutIndex == 16)) {
							localInteger = Integer.valueOf(220);
						}
						if (((modifiers & 0x2) == 2) || ((modifiers & 0x20) == 32)) {
							enableShift(false);
							this.shiftModified = true;
						}

						if (((modifiers & 0x40) == 64) && (!JViewerApp.getInstance().getJVMenu()
								.getMenuItem("KeyboardHoldRightAltKey").isSelected())) {
							enableAltGr(false);
							this.altGrModified = true;
						}

						if (((modifiers & 0x1) == 1) && (!JViewerApp.getInstance().getJVMenu()
								.getMenuItem("KeyboardHoldLeftCtrlKey").isSelected())) {
							enableLeftCTRL(false);
							this.leftCTRLModified = true;
						}
					}
				}
			}
			if (i < 0) {

				int j = altGrCharSet[this.hostLayoutIndex].indexOf(paramChar);
				if (j >= 0)
					i = Integer.parseInt(altGrIndex[this.hostLayoutIndex][j]);
				if (i >= 0) {
					i -= 16;

					localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
					if ((modifiers & 0x40) != 64)
						enableAltGr(true);
					if (((modifiers & 0x2) == 2) || ((modifiers & 0x20) == 32)) {
						enableShift(false);
						this.shiftModified = true;
					}

				} else if (i < 0) {
					int k = shiftedAltGrCharSet[this.hostLayoutIndex].indexOf(paramChar);
					if (k >= 0)
						i = Integer.parseInt(shiftedAltGrIndex[this.hostLayoutIndex][k]);
					if (i >= 0) {
						i -= 16;

						localInteger = (Integer) this.keyCodeMap.get(Integer.valueOf(i));
						if ((modifiers & 0x40) != 64) {
							enableAltGr(true);
						}
						if (((modifiers & 0x2) != 2) && ((modifiers & 0x20) != 32)) {

							enableShift(true);
							this.shiftModified = true;
						}
					}
				}

				if (((modifiers & 0x1) == 1) || ((modifiers & 0x10) == 16)) {

					if (!this.altGrHeldDown) {
						localInteger = Integer.valueOf(getLocalKeyCode(paramInt));
					}
				}

				if (localInteger == null) {
					localInteger = Integer.valueOf(isCommonKey(paramInt));
				}
			}

			if (localInteger == null) {
				localInteger = Integer.valueOf(0);
			}
			this.pressedKeyCode = localInteger.intValue();
			this.pressedKeyChar = paramChar;
		} else {
			if ((paramChar == this.pressedKeyChar) && (paramInt != 16) && (paramInt != 17)) {
				localInteger = Integer.valueOf(this.pressedKeyCode);
			} else
				localInteger = Integer.valueOf(paramInt);
			if (localInteger.intValue() == 0) {

				localInteger = Integer.valueOf(isCommonKey(paramInt));
			}
			if (((modifiers & 0x2) == 2) || ((modifiers & 0x20) == 32) || (this.shiftModified)) {
				if (this.shiftHeldDown) {
					enableShift(true);
				} else
					enableShift(false);
			}
			if (((modifiers & 0x40) == 64) || (this.altGrModified)) {
				if (this.altGrHeldDown) {
					enableAltGr(true);
				} else if (!JViewerApp.getInstance().getJVMenu().getMenuItem("KeyboardHoldRightAltKey").isSelected()) {
					enableAltGr(false);
				}
			}
			if (((modifiers & 0x1) == 1) || (this.leftCTRLModified)) {
				if (this.leftCTRLHeldDown) {
					enableLeftCTRL(true);
				} else if (!JViewerApp.getInstance().getJVMenu().getMenuItem("KeyboardHoldRightCtrlKey").isSelected()) {
					enableLeftCTRL(false);
				}
			}
			this.shiftModified = false;
			this.altGrModified = false;
			this.leftCTRLModified = false;
			this.autoKeybreakModeOn = false;
			this.pressedKeyCode = 0;
			this.pressedKeyChar = 0;
		}
		return localInteger.intValue();
	}

	private void enableShift(boolean paramBoolean) {
		if (paramBoolean) {
			modifiers = (byte) (modifiers | 0x2);
			modifiers = (byte) (modifiers | 0x20);
		} else {
			modifiers = (byte) (modifiers & 0xFFFFFFFD);
			modifiers = (byte) (modifiers & 0xFFFFFFDF);
		}
	}

	private void enableAltGr(boolean paramBoolean) {
		if (paramBoolean) {
			modifiers = (byte) (modifiers | 0x40);
		} else {
			modifiers = (byte) (modifiers & 0xFFFFFFBF);
		}
	}

	private void enableLeftCTRL(boolean paramBoolean) {
		if (paramBoolean) {
			modifiers = (byte) (modifiers | 0x1);
		} else {
			modifiers = (byte) (modifiers & 0xFFFFFFFE);
		}
	}

	private int isCommonKey(int paramInt) {
		int i = 0;

		for (int j = 0; j < this.commonKeys.length; j++)
			if (paramInt == this.commonKeys[j]) {
				i = paramInt;
				break;
			}
		return i;
	}

	public void setAutoKeybreakMode(boolean paramBoolean) {
		this.autoKeybreakModeOn = paramBoolean;
	}

	public boolean getAutoKeybreakMode() {
		return this.autoKeybreakModeOn;
	}

	protected int getLocalKeyCode(int paramInt) {
		Integer localInteger = null;
		if (this.localKeyCodeMap != null) {
			localInteger = (Integer) this.localKeyCodeMap.get(Integer.valueOf(paramInt));
		}
		if (localInteger == null)
			localInteger = Integer.valueOf(paramInt);
		return localInteger.intValue();
	}

	public HashMap<Integer, Integer> getLocalKeyCodeMap() {
		return this.localKeyCodeMap;
	}

	public void setLocalKeyCodeMap(HashMap<Integer, Integer> paramHashMap) {
		this.localKeyCodeMap = paramHashMap;
	}

	public int getHostLayoutIndex() {
		return this.hostLayoutIndex;
	}

	public void setHostLayoutIndex(int paramInt) {
		this.hostLayoutIndex = paramInt;
	}
}
